#ifndef TIMESTAMP_H
#define TIMESTAMP_H

#include <cstdint>
#include <string>

namespace Logger
{
    class Timestamp
    {
    public:
        Timestamp() : microSecondsSinceEpoch_(0)
        {
        }

        explicit Timestamp(int64_t microSecondsSinceEpoch)
            : microSecondsSinceEpoch_(microSecondsSinceEpoch)
        {
        }

        static Timestamp now();
        std::string toString() const;
        std::string toFormattedString(bool showMicroseconds = false) const;

        static Timestamp invalid() // 时间戳失效，返回0
        {
            return Timestamp{};
        }

        int64_t getMicroSecondsSinceEpoch() const
        {
            return this->microSecondsSinceEpoch_;
        }

        static const int sMicroSecondsPerSecond = 1000 * 1000; // 1秒 = 1000 * 1000 微秒
        time_t getSecondsSinceEpoch() const
        {
            return static_cast<time_t>(this->microSecondsSinceEpoch_ / sMicroSecondsPerSecond);
        }

    private:
        int64_t microSecondsSinceEpoch_;
    };

    // // 定时器需要比较时间戳，需要重载运算符
    // inline bool operator<(Timestamp lhs, Timestamp rhs)
    // {
    //     return lhs.getMicroSecondsSinceEpoch() < rhs.getMicroSecondsSinceEpoch();
    // }

    // inline bool operator==(Timestamp lhs, Timestamp rhs)
    // {
    //     return lhs.getMicroSecondsSinceEpoch() == rhs.getMicroSecondsSinceEpoch();
    // }

    // // 重复定时任务需要时间戳增加
    // inline Timestamp addTime(Timestamp timestamp, double seconds)
    // {
    //     int64_t delta = static_cast<int64_t>(seconds * Timestamp::sMicroSecondsPerSecond);
    //     return Timestamp(timestamp.getMicroSecondsSinceEpoch() + delta);
    // }
} // namespace Logger

#endif // TIMESTAMP_H